//
//  NetworkHandle.m
//  抓包验证工具
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NetworkHandle.h"

@implementation NetworkHandle


+ (void)GETRequestWithURLString:(NSString *)string completion:(void (^)(id))block {
    //1.筛选中文
    NSString *newStr = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    
    //2.转化为URL
    NSURL *url = [NSURL URLWithString:newStr];
    
    //3.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    NSLog(@"lalallalallalallala%@", request);
    
    //4.创建会话对象
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
                             
      //5.创建网络任务
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        block(data);
//        NSLog(@"这是data~~~~~%@", data);
    }];
    
    [task resume];
}



+ (void)POSTRequestWithURLString:(NSString *)string body:(NSString *)body completion:(void (^)(id))block{
    
    NSString *str = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    
    NSURL *url = [NSURL URLWithString:str];
    

    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];
//    [request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
    request.HTTPMethod = @"POST";
    
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = data;
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            block(dic);
            
        });
    }];
    
    [task resume];
    
}

+ (void)POSTRequestWithURLStringRRRH:(NSString *)string body:(NSData *)body completion:(void (^)(id))block {
    NSString *str = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    
    NSURL *url = [NSURL URLWithString:str];
    
    
    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Context-Type"];
//    [request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];
//    [request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
    request.HTTPMethod = @"POST";
    
    request.HTTPBody = body;
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        block(dic);
    }];
    
    [task resume];

}

@end
